home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 June / EnigmA AMIGA RUN 19 (1997)(G.R. Edizioni)(IT)[!][issue 1997-06][EAR-CD III].iso / recent1 / apic1805.lha / APIC / examples / HelloWorld.src < prev    next >
Text File  |  1997-05-10  |  3KB  |  180 lines

  1.  
  2.  
  3.  
  4.  
  5. ;this is a hello world program written in microchip assembler,
  6. ;
  7. ;the string is transmitted serial to an terminal. the baudrate is 
  8. ;determined by the value of "bitcntr", loaded with "delay"
  9. ;19200 at 4 MHz clock speed needs at all 52 cycles pro bit
  10. ;
  11. ;after sending all characters pic runs an endless loop, a reset is necessary
  12. ;to send string again. this reset is performed by a watchdog time out. the
  13. ;watchdogtimer is set to 2.3 seconds (128 * 18 ms)
  14. ;
  15. ;string lenght is defined by a zero byte at end of string
  16. ;
  17. ;
  18. ; http://linux.rz.fh-hannover.de/~duesterb/
  19.  
  20.  
  21.  
  22.  
  23.         list    p=pic16c84, r=dec, s=off    ;radix = decimal
  24.                             ;case sensitivity=off
  25.  
  26. back
  27.  
  28.  
  29. clockspeed    =    4000000                ;clockspeed is 4 Mhz
  30. baudrate    =    19200                ;enter baudrate here
  31.  
  32. delay        =    (clockspeed/4/baudrate-12)/4    ;value for baudrate, 12 cycles fixed, 4 cycles delay
  33.  
  34. ram        =    0ch        ;beginning of RAM register
  35.  
  36. #define    happy
  37.  
  38.  
  39.     CBLOCK ram
  40.  
  41.     delaycntr                ;bit dely counter
  42.     charcntr                ;counter shows actual character of string
  43.     bitcntr                    ;counter shows actual bit of byte
  44.     xmitdata                ;data to transmit
  45.  
  46.     ENDC
  47.  
  48.  
  49.  
  50.  
  51. PC        =    02h        ;programcounter
  52. PortA        =    05h        ;register 05h is PortA
  53.     
  54.  
  55.  
  56. #define        TXD    PortA,0            ;TXD line is bit 0 from Port A
  57. #define        c    03h,0            ;carry bit is bit 0 from status register
  58. #define        z    03h,2            ;zero bit is bit 2 from status register
  59.  
  60.  
  61.  
  62.  
  63. ; Remember to change device info if programming a different PIC. Do not use RC
  64. ; devices. Their clock speed is not sufficiently accurate or stable for serial
  65. ; communication. 
  66.  
  67.  
  68.  
  69. begin        movlw    0        ;move 0 to tristate register, set port to output.
  70.         tris    PortA
  71.  
  72.         clrf    charcntr    ;we start at the first character
  73.                      
  74.  
  75. send_byte    movlw    8h        ;Eight bits in a byte. 
  76.         movwf    bitcntr
  77.  
  78.         call    string        ;Get character from string. 
  79.         movwf    xmitdata    ;Put character into the transmit byte. 
  80.  
  81.         movf    xmitdata,f    ;test xmitdata on zero
  82.         btfsc    z
  83.         goto    endless        ;do endless loop if xmitdata was zero
  84.         
  85.  
  86.         bsf    TXD        ;set TXD line, dirct connection
  87.  
  88.         incf    charcntr,f    ;next position of string. 
  89.  
  90.         call    bitdelay    ;Start bit. ((delay * 4) + 4) cycles
  91.  
  92.  
  93.  
  94. xmit        rrf    xmitdata,f    ;Rotate right moves data bits into
  95.                     ;carry, starting with bit 0. 
  96.  
  97.         btfsc    c        ;clear TXD if carry bit is set
  98.         bcf    TXD        
  99.         btfss    c        ;set TXD if carry bit is clear
  100.         bsf    TXD
  101.  
  102.  
  103.  
  104.         call    bitdelay    ;Data bit.
  105.  
  106.         decfsz    bitcntr,f    ;Not eight bits yet? Send next data bit
  107.         goto    xmit
  108.  
  109.         bcf    TXD        ;clear TXD
  110.  
  111.  
  112.  
  113.         call    bitdelay    ;Stop bit. ((delay * 4) + 4) cycles
  114.         call    bitdelay    ;Stop bit. ((delay * 4) + 4) cycles
  115.  
  116.  
  117.         goto    send_byte    ;goto send_byte if line feed is not send
  118.  
  119.  
  120.  
  121.  
  122. endless        goto    endless        ;Endless loop. Reset controller to run
  123.                     ;program again.
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. ; To change the baud rate, substitute a new value for bitdelay at the beginning of
  135. ; this program. 
  136.  
  137.  
  138.  
  139. bitdelay    movlw    delay        ;this bitdelay delays ((delay * 4) + 4) cycles
  140.         movwf    delaycntr
  141. :loop        nop
  142.         decfsz    delaycntr,f
  143.         goto    :loop        ;this is an local loop
  144.         retlw    0
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. string        movf    charcntr,w
  153.         addwf    PC,f        ;String consisting of "Hello world"
  154.                     ;followed by a linefeed. 
  155.  
  156.  
  157.  
  158.  
  159.         ifdef    happy
  160.  
  161.           space    2        ;2 blank lines to listfile
  162.           messg "I`m happy"    ;message to listfile
  163.           space    2
  164.  
  165.           retlw    "Hello world",0ah,0ah    ;string, two linefeeds
  166.  
  167.         else
  168.  
  169.           error    "don`t worry, be happy !"
  170.  
  171.         endif
  172.  
  173.  
  174.         retlw    0dh,0        ;one cr, Zerobyte => stop sending
  175.  
  176.  
  177.  
  178.  
  179.         end
  180.